<?php

class ReportController extends GTL_Action {

    public function init() {
        parent::init();
       
    }

    public function indexAction() {
        $this->_helper->redirector('download', 'report', '');
    }
  
  
    public function downloadAction() {

        //Initialize
        $successmessage = "";
        $errorMessageArray = Array();
        $data = array();
        $adminData = Array();
        $ins_sum = "";
        //Load Report Form
        $reportForm = new Form_ReportForm();
        $agentModel = new Model_Agent();
        $agentData =$agentModel -> getagentreportArray();
       

         //Load the agent array for the select box in form 
        $reportForm->agent_name->addMultiOptions($agentData);
        
        //Populate form 
        if ($this->getRequest()->isPost()) {

            //Check for Errors
            $postedData = $this->_request->getPost();
            
            if (!$reportForm->isValid($postedData)) {
                $errorMessage = $reportForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {
                
                if (count($errorMessageArray) > 0) {
                } else {
                    //download
                    $agent_name = $postedData['agent_name'];
                    $start_date = $postedData['start_date'];
                    $end_date = $postedData['end_date'];
                    $output = fopen('php://output', 'w');

                   
                    $loanModel = new Model_Loan();
                    $installmentModel = new Model_Installment();
                    /* $loanalldata = $loanModel->fetchEntryByViewId($ln_id);
                    $installmentData = $loanModel->getInstallmentData($ln_id);
                    */
                    $where ="ln_ag_id = '".$agent_name."' and ln_startdate Between '".$start_date."' AND '".$end_date."'  ";
                    $loandata = $loanModel->fetchLoan($where);
                    
                    $agentModel = new Model_Agent();
                    $agentdata = $agentModel->fetchEntryById($agent_name);
                    
                   
                    

                    fputcsv($output, array('Agent Detail'));   
                    fputcsv($output, array('Agent Name:',$agentdata['ag_name']));   
                    fputcsv($output, array('Mobile No:',$agentdata['ag_mobile']));
                    fputcsv($output, array('Start Date:',$start_date));
                    fputcsv($output, array('End Date:',$end_date));
                    
                    fputcsv($output, array(''));
                    fputcsv($output, array(''));

                    fputcsv($output, array('Loan Detail'));  

                    fputcsv($output, array('Customer Name','Loan Name','Loan Start Date','Loan End Date','Guarantor Name','Guarantor Number','Loan Note', 'Loan Amount','Loan Advanced Amount','Total Installment Amount','Installment Count'));

                    $row  = array();
                    foreach ($loandata as $key => $value) {
                        $ins_sum = '';
                        $where = "ins_ln_id = ".$value['ln_id'];
                        $installmentData = $installmentModel->fetchInstallment($where);
                        $installmentcount = $installmentModel->getInstallmentCount($where);
                        foreach($installmentData as $ins_key =>$ins_value){
                            $ins_sum += $ins_value['ins_amount'];
                        }
                        
                        $row['cus_name'] = $value['cus_name'];
                        $row['ln_name'] = $value['ln_name'];
                        $row['ln_startdate'] = $value['ln_startdate'];
                        $row['ln_enddate'] = $value['ln_enddate'];
                        $row['ln_guarantor_name'] = $value['ln_guarantor_name'];
                        $row['ln_guarantor_mobile'] = $value['ln_guarantor_mobile'];
                        $row['ln_note'] = $value['ln_note'];
                        $row['ln_amount'] = 'Rs.'.$value['ln_amount'];
                        $row['ln_advance'] = 'Rs.'.$value['ln_advance'];
                        
                        $row['ins_amount'] = 'Rs.'.$ins_sum;
                        $row['ins_count'] = $installmentcount;
                        fputcsv($output, $row);   
                    }
                    $ln_name = str_replace(" ","-", $agentdata['ag_name']);
                    $filename =$ln_name."_".rand();
                    header('Content-Type: text/csv; charset=utf-8');
                    header('Content-Disposition: attachment; filename='.$filename.'.csv');
                    exit;
                }
            }
        }

       
         
            $this->view->errorMessageArray = $errorMessageArray;
            $this->view->succesMessage = $successmessage;
            $this->view->form = $reportForm;
            
       
    }

}

?>
